package com.lynch.mp3;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.lynch.model.Mp3Info;
import com.lynch.util.FileUtils;

public class LocalMp3ListActivity extends ListActivity {
	
	private List<Mp3Info> mp3List = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.localmp3list);
	}
	
	@Override
	protected void onResume() {
		
		FileUtils fileUtils = new FileUtils();
		mp3List = fileUtils.getMp3Files("myMp3/");
		List<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
		Iterator<Mp3Info> iterator = mp3List.iterator();
		while (iterator.hasNext()) {
			Mp3Info mp3Info = iterator.next();
			HashMap<String,String> map = new HashMap<String,String>();
			map.put("mp3Name", mp3Info.getMp3Name());
			map.put("mp3Size", mp3Info.getMp3Size());
			list.add(map);
		}
		
		SimpleAdapter simpleAdapter = new SimpleAdapter(this,list,R.layout.mp3infoitem,new String[]{"mp3Name","mp3Size"},new int[]{R.id.mp3Name,R.id.mp3Size});
		setListAdapter(simpleAdapter);
		super.onResume();
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		if(mp3List!=null){
			Mp3Info mp3Info = mp3List.get(position);
			Intent intent = new Intent();
			intent.putExtra("mp3Info", mp3Info);
			intent.setClass(this, PlayerActivity.class);
			startActivity(intent);
		}
	}
	
	
	
}
